package com.mall.product.service;

import com.mall.common.util.JsonResult;
import com.mall.model.Product;


import com.situ.mall.notification.spi.model.DTO.PriceChangeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

/**
 * 商品变化通知服务
 * 当商品信息发生变化时，自动发送通知
 */
@Slf4j
@Service
public class ProductNotificationService {
    
    @Autowired
    private NotificationFeignService notificationFeignService;
    
    /**
     * 发送商品价格变化通知
     */
    public void sendPriceChangeNotification(Product product, Double oldPrice, Double newPrice, int[] userIds) {
        try {
            PriceChangeDTO dto = new PriceChangeDTO();
            dto.setProductId(Math.toIntExact(product.getId()));
            dto.setProductName(product.getName());
            dto.setOldPrice(oldPrice);
            dto.setNewPrice(newPrice);
            dto.setIds(userIds);
            
            ResponseEntity<JsonResult<?>> response = notificationFeignService.PriceChange(dto);
            JsonResult<?> result = response.getBody();
            
            if (result != null && result.isSuccess()) {
                log.info("商品价格变化通知发送成功，商品ID: {}, 价格变化: {} -> {}", 
                        product.getId(), oldPrice, newPrice);
            } else {
                log.error("商品价格变化通知发送失败，商品ID: {}", product.getId());
            }
        } catch (Exception e) {
            log.error("发送商品价格变化通知异常，商品ID: {}", product.getId(), e);
        }
    }
    


}

